var mods = [
  'element', 'sidebar', 'mockjs', 'select',
  'tabs', 'menu', 'route', 'utils', 'component', 'kit'
];

layui.define(mods, function(exports) {
  var element = layui.element,
    utils = layui.utils,
    $ = layui.jquery,
    _ = layui.lodash,
    route = layui.route,
    tabs = layui.tabs,
    layer = layui.layer,
    menu = layui.menu,
    component = layui.component,
    kit = layui.kit;


  var Admin = function() {
    this.config = {
      elem: '#app',
      loadType: 'SPA'
    };
    this.version = '1.0.0';
  };

  Admin.prototype.ready = function(callback) {
    var that = this,
      config = that.config;

    // 初始化加载方式
    var getItem = utils.localStorage.getItem;
    var setting = getItem("KITADMIN_SETTING_LOADTYPE");
    if (setting !== null && setting.loadType !== undefined) {
      config.loadType = setting.loadType;
    }

    kit.set({
      type: config.loadType
    }).init();

    // 初始化路由
    _private.routeInit(config);
    // 初始化选项卡
    if (config.loadType === 'TABS') {
      _private.tabsInit();
    }
    // 初始化左侧菜单   -- 请先初始化选项卡再初始化菜单
    _private.menuInit(config);
    // 跳转至首页
    if (location.hash === '') {
      utils.setUrlState('主页', '#/index');
    }

    // 处理 sidebar
    // layui.sidebar.render({
    //   elem: '#ccleft',
    //   //content:'', 
    //   title: '这是左侧打开的栗子',
    //   shade: true,
    //   // shadeClose:false,
    //   direction: 'left',
    //   dynamicRender: true,
    //   url: 'views/table/teble2.html',
    //   width: '50%', //可以设置百分比和px
    // });

    // $('#cc').on('click', function() {
    //   var that = this;
    //   layui.sidebar.render({
    //     elem: that,
    //     //content:'', 
    //     title: '这是表单盒子',
    //     shade: true,
    //     // shadeClose:false,
    //     // direction: 'left'
    //     dynamicRender: true,
    //     url: 'views/form/index.html',
    //     width: '50%', //可以设置百分比和px
    //   });
    // });
    // 监听头部右侧 nav
    component.on('nav(header_right)', function(_that) {
      var target = _that.elem.attr('kit-target');
      if (target === 'setting') {
        // 绑定sidebar
        layui.sidebar.render({
          elem: _that.elem,
          //content:'', 
          title: '设置',
          shade: true,
          // shadeClose:false,
          // direction: 'left'
          dynamicRender: true,
          url: 'views/setting.html',
          // width: '50%', //可以设置百分比和px
        });
      }
      if (target === 'help') {
        layer.alert('技术支持：首都信息');
      }
    });

    // 注入mock
    layui.mockjs.inject(APIs);

    // 初始化渲染
    if (config.loadType === 'SPA') {
      route.render();
    }

    // 执行回调函数
    typeof callback === 'function' && callback();
  }

  var _private = {
    routeInit: function(config) {
      var that = this;
      // route.set({
      //   beforeRender: function (route) {
      //     if (!utils.oneOf(route.path, ['/user/table', '/user/table2', '/'])) {
      //       return {
      //         id: new Date().getTime(),
      //         name: 'Unauthorized',
      //         path: '/exception/403',
      //         component: 'views/exception/403.html'
      //       };
      //     }
      //     return route;
      //   }
      // });
      // 配置路由
      var routeOpts = {
        // name: 'kitadmin',
        r: [{
          path: '/user/index',
          component: '/views/user/index.html',
          name: '用户列表',
          children: [{
            path: '/user/create',
            component: 'views/user/create.html',
            name: '新增用户',
          }, {
            path: '/user/edit',
            component: 'views/user/edit.html',
            name: '编辑用户',
          }]
        }],
        routes: [
			{//首页
				path: '/index',
				component: 'views/app.html',
				name: 'personal',
			},
			{//人员标签管理
			path: '/personnelTag/personal',
			component: 'views/pages/personnel-tag-manage/tag-personal/index.html',
			name: 'personal',
		},{
			path: '/personnelTag/type',
			component: 'views/pages/personnel-tag-manage/tag-type/index.html',
			name: 'type',
		},{ //人才库
			path: '/personnelBase/baseManage',
			component: 'views/pages/personnel-base-manage/personnel-base-manage/index.html',
			name: 'baseManage',
		},{
			path: '/personnelBase/apply',
			component: 'views/pages/personnel-base-manage/personal-apply/index.html',
			name: 'apply',
		},{
			path: '/personnelBase/check',
			component: 'views/pages/personnel-base-manage/personal-check/index.html',
			name: 'check',
		},{ //借调人员管理
			path: '/secondedManage/secondedApply',
			component: 'views/pages/seconded-manage/seconded-apply/index.html',
			name: 'secondedApply',
		},{  
			path: '/secondedManage/secondedCheck',
			component: 'views/pages/seconded-manage/seconded-checked/index.html',
			name: 'secondedCheck',
		},{  
			path: '/personnelBase/secondedManage',
			component: 'views/pages/seconded-manage/seconded-manage/index.html',
			name: 'secondedManage',
		},{  //日常管理
			path: '/dailyManage/foreignManage',
			component: 'views/pages/daily-manage/foreign-manage/index.html',
			name: 'foreignManage',
		},{  //日常管理
			path: '/dailyManage/fileManage',
			component: 'views/pages/daily-manage/file-manage/index.html',
			name: 'foreignManage',
		},{  //日常管理
			path: '/dailyManage/salaryList',
			component: 'views/pages/daily-manage/money-manage/salary-list/index.html',
			name: 'salaryList',
		},{  //日常管理
			path: '/dailyManage/insuranceList',
		component: 'views/pages/daily-manage/money-manage/insurance-list/index.html',
			name: 'insuranceList',
		},{  //日常管理
			path: '/dailyManage/xiujiaManage',
			component: 'views/pages/daily-manage/xiujia-manage/index.html',
			name: 'xiujiaManage',
		},{  //日常管理
			path: '/dailyManage/noticeManage',
			component: 'views/pages/daily-manage/notice-manage/index.html',
			name: 'noticeManage',
		},{  //考核管理
			path: '/assessManage/assessManage',
			component: 'views/pages/assess-manage/assess-manage/index.html',
			name: 'assessManage',
		} ,{  //培训管理
			path: '/tranningManage/tranningClass',
			component: 'views/pages/tranning-manage/tranning-class/index.html',
			name: 'tranningClass',
		},{  //培训管理
			path: '/tranningManage/tranningPeople',
			component: 'views/pages/tranning-manage/tranning-people/index.html',
			name: 'tranningPeople',
		},{  //离退休管理
			path: '/retireManage/retirePeople',
			component: 'views/pages/retire-manage/retire-people/index.html',
			name: 'retirePeople',
		},{  //离退休管理
			path: '/retireManage/retireList',
			component: 'views/pages/retire-manage/retire-list/index.html',
			name: 'retireList',
		},{  //任免上会管理
			path: '/meetingManage/inputPeople',
			component: 'views/pages/meeting-manage/input-people/index.html',
			name: 'inputPeople',
		},{  //任免上会管理
			path: '/meetingManage/meetingTask',
			component: 'views/pages/meeting-manage/meeting-task/index.html',
			name: 'meetingTask',
		},{  //任免上会管理
			path: '/meetingManage/meetingPeople',
			component: 'views/pages/meeting-manage/meeting-people/index.html',
			name: 'meetingPeople',
		},{  //任免上会管理
			path: '/meetingManage/meetingConfirm',
			component: 'views/pages/meeting-manage/meeting-confirm/index.html',
			name: 'meetingConfirm',
		},{  //任免上会管理
			path: '/meetingManage/meetingBatchList',
			component: 'views/pages/meeting-manage/meeting-batchList/index.html',
			name: 'meetingBatchList',
		},{  //任免上会管理
			path: '/meetingManage/meetingBatchPeople',
			component: 'views/pages/meeting-manage/meeting-batchPeople/index.html',
			name: 'meetingBatchPeople',
		},{  //任免上会管理
			path: '/meetingManage/meetingList',
			component: 'views/pages/meeting-manage/meeting-list/index.html',
			name: 'meetingList',
		},{  //监察证管理
			path: '/superviseManage/cardApply',
			component: 'views/pages/supervise-manage/card-apply/index.html',
			name: 'cardApply',
		},{  //监察证管理
			path: '/superviseManage/cardChecked',
			component: 'views/pages/supervise-manage/card-checked/index.html',
			name: 'cardChecked',
		},{  //监察证管理
			path: '/superviseManage/cardShenpi',
			component: 'views/pages/supervise-manage/card-shenpi/index.html',
			name: 'cardShenpi',
		},{  //综合统计查询
			path: '/totalManage/totalQuery',
			component: 'views/pages/total-query/total-query/index.html',
			name: 'totalQuery',
		},{  //综合统计查询
			path: '/totalManage/compilationList',
			component: 'views/pages/total-query/compilation-list/index.html',
			name: 'compilationList',
		} 
		]
      };
      if (config.loadType === 'TABS') {
        routeOpts.onChanged = function() {
          // 如果当前hash不存在选项卡列表中
          if (!tabs.existsByPath(location.hash)) {
            // 新增一个选项卡
            that.addTab(location.hash, new Date().getTime());
          } else {
            // 切换到已存在的选项卡
            tabs.switchByPath(location.hash);
          }
        }
      }
      // 设置路由
      route.setRoutes(routeOpts);
      return this;
    },
    addTab: function(href, layid) {
      var r = route.getRoute(href);
      if (r) {
        tabs.add({
          id: layid,
          title: r.name,
          path: href,
          component: r.component,
          rendered: false,
          icon: '&#xe62e;'
        });
      }
    },
    menuInit: function(config) {
      var that = this;
      // 配置menu
      menu.set({
        dynamicRender: false,
        isJump: config.loadType === 'SPA',
        onClicked: function(obj) {
          if (config.loadType === 'TABS') {
            if (!obj.hasChild) {
              var data = obj.data;
              var href = data.href;
              var layid = data.layid;
              that.addTab(href, layid);
            }
          }
        },
        elem: '#menu-box',
        remote: {
          url: '/api/getmenus',
          method: 'post'
        },
        cached: false
      }).render();
      return that;
    },
    tabsInit: function() {
      tabs.set({
        onChanged: function(layid) {
          // var tab = tabs.get(layid);
          // if (tab !== null) {
          //   utils.setUrlState(tab.title, tab.path);
          // }
        }
      }).render(function(obj) {
        // 如果只有首页的选项卡
        if (obj.isIndex) {
          route.render('#/');
        }
      });
    }
  }

  var admin = new Admin();
  admin.ready(function() {
    console.log('Init successed.');
  });

  //输出admin接口
  exports('admin', {});
});